var MongoClient=require('mongodb').MongoClient;
var Config=require('./config.js');
const ObjectID=require('mongodb').ObjectID;

// console.log(Config);

class Db{

    static getInstance(){
        if(!Db.instance){
            Db.instance=new Db();
        }
        return Db.instance;
        // 封装静态方法使多个实例存在的时候也可以子进行一个连接
    }

    constructor() {
        this.dbClient=null;

        this.connect();
    }

    connect(){
        let _this=this;
        // 避免箭头函数的this指向问题
        return new Promise(function (resolve, reject){
            if(!_this.dbClient){
                MongoClient.connect(Config.dbUrl,(err,client)=>{
                    if(err){
                        reject(err);
                    }else{
                        var db=client.db(Config.dbName)
                        _this.dbClient=db;
                        resolve(_this.dbClient)
                    }
                })
            }else {
                resolve(_this.dbClient)
            }

        })
    }
    find(collectionName,json){



        return new Promise((resolve, reject) =>{
            this.connect().then((db)=>{
                var result= db.collection(collectionName).find(json);
                result.toArray((err,docs)=>{
                    if(err){
                        reject(err);
                    }
                    // console.log(docs);
                    resolve(docs);
                })
            })

        })

    }
    update(collectionName,json1,json2){
        return new Promise((resolve, reject) => {
            this.connect().then((db)=>{
                db.collection(collectionName).updateOne(json1,{
                    $set:json2
                },(err,result)=>{
                    if(err){
                        reject(err);

                    }else {
                        resolve(result);
                    }
                })
            })

        })
    }
    insert(collectionName,json){
        return new Promise((resolve, reject) => {
            this.connect().then((db)=>{
                db.collection(collectionName).insertOne(json,(err,result)=>{
                    if(err){
                        reject(err);

                    }else {
                        resolve(result);
                    }
                })
            })
        })
    }

    remove(collectionName,json){

        return new Promise((resolve, reject) => {
            this.connect().then((db)=>{
                db.collection(collectionName).removeOne(json,(err,result)=>{
                    if(err){
                        reject(err);

                    }else {
                        resolve(result);
                    }
                })
            })
        })

    }
    getObjectID(id){
        return new ObjectID(id);
    }

}

/*


var mydb=Db.getInstance();




setTimeout(()=>{
    console.time('s1');
    mydb.find('user',{}).then((data)=>{
        console.log(data);
        console.timeEnd('s1');
    })
},100)


setTimeout(()=>{
    console.time('s2');
    mydb.find('user',{}).then((data)=>{
        console.log(data);
        console.timeEnd('s2');
    })
},2000)


*/

module.exports=Db.getInstance();
